package com.shanjupay.merchant.service;

import com.alibaba.fastjson.JSONObject;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.merchant.utils.HttpClientTools;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SmsServiceimplVer implements SmsService {

    @Value("${sms.url}")
    String url="";

    @Value("${sms.effectiveTime}")
    String effectiveTime="";

    String uri="/sailing/generate?name=sms&effectiveTime=%s";

    String checkVerifiyCodeUri="/sailing/verify?name=sms&verificationCode=%s&verificationKey=%s";
    @Override
    public JSONObject sendMsg(String phone) {

         //获取到发送短信的地址
        String sendMsgUrl=url+String.format(uri,effectiveTime);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("mobile",phone);

        //调用接口
        return HttpClientTools.httpPost(sendMsgUrl,jsonObject);

    }

    @Override
    public void checkVerifiyCode(String verifiyKey, String verifiyCode) throws BusinessException {

        String verifiyCodeUrl=url+String.format(checkVerifiyCodeUri,verifiyCode,verifiyKey);
        JSONObject jsonObject = HttpClientTools.httpPost(verifiyCodeUrl, "");

        Boolean result = (Boolean) jsonObject.get("result");
        if (!result){
            throw new RuntimeException("校验验证码失败");
        }
    }
}
